package com.hhp.cloud.shop.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.TypeReference;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.hhp.cloud.shop.admin.common.constant.SystemConstants;
import com.hhp.cloud.shop.admin.pojo.vo.menu.RouteVO;
import com.hhp.cloud.shop.common.core.constant.GlobalConstants;
import org.apache.commons.compress.utils.Lists;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.hhp.cloud.shop.admin.dao.MenuDao;
import com.hhp.cloud.shop.admin.pojo.entity.MenuEntity;
import com.hhp.cloud.shop.admin.service.MenuService;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


@Service("menuService")
public class MenuServiceImpl extends ServiceImpl<MenuDao, MenuEntity> implements MenuService {

    @Override
    @CacheEvict(cacheNames = "system", key = "'routeList'")
    public void cleanCache() {

    }

    @Override
    @Cacheable(cacheNames = "system", key = "'routes'")
    public List<RouteVO> listRouteMenus() {
        List<MenuEntity> menuList = this.baseMapper.listRoutes();
        List<RouteVO> list = recursionRoute(SystemConstants.ROOT_MENU_ID, menuList);
        return list;
    }

    /**
     * 递归生成菜单路由层级列表
     *
     * @param parentId 父级ID
     * @param menuList 菜单列表
     * @return
     */
    private List<RouteVO> recursionRoute(Long parentId, List<MenuEntity> menuList) {
        List<RouteVO> list = new ArrayList<>();
        Optional.ofNullable(menuList).ifPresent(menus -> menus.stream().filter(menu -> menu.getParentId().equals(parentId))
                .forEach(menu -> {
                    RouteVO routeVO = new RouteVO();
                    if(parentId.equals(SystemConstants.ROOT_MENU_ID)){
                        routeVO.setType(0);
                    }else{
                        routeVO.setType(1);
                    }
                    routeVO.setId(menu.getId());
                    routeVO.setParentId(menu.getParentId());
                    /*routeVO.setName(menu.getId() + ""); // 根据name路由跳转 this.$router.push({path:xxx})*/
                    routeVO.setName(menu.getName() + ""); // 根据name路由跳转 this.$router.push({path:xxx})
                    routeVO.setPath(menu.getPath());    // 根据path路由跳转 this.$router.push({name:xxx})
                    routeVO.setRedirect(menu.getRedirect());
                    routeVO.setComponent(menu.getComponent());
                    routeVO.setSort(menu.getSort());
                    RouteVO.Meta meta = new RouteVO.Meta();
                    meta.setTitle(menu.getName());
                    meta.setIcon(menu.getIcon());
                    meta.setRoles(menu.getRoles());
                    routeVO.setMeta(meta);
                    // 菜单显示隐藏
                    routeVO.setHidden(!GlobalConstants.STATUS_YES.equals(menu.getVisible()));
                    //菜单显示隐藏使用meta里的hideMenu来控制
                    meta.setHideMenu(!GlobalConstants.STATUS_YES.equals(menu.getVisible()));
                    List<RouteVO> children = recursionRoute(menu.getId(), menuList);
                    routeVO.setChildren(children);
                    if (CollectionUtil.isNotEmpty(children)) {
                        routeVO.setAlwaysShow(Boolean.TRUE);
                    }
                    list.add(routeVO);
                }));
        return list;
    }
}